/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: service.h
 *
 * Purpose: service endain base class
 *
 * Developer:
 *   wen.gu , 2020-12-24
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __COM_SERVICE_H__
#define __COM_SERVICE_H__

#include <string>
#include <functional>
#include <memory>
#include "collie/core/types.h"
#include "collie/core/errors.h"
#include "collie/com/network_binding.h"

namespace collie
{
namespace com
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/

class MethodHandle
{
public:
    MethodHandle(){}
    virtual ~MethodHandle(){}
public:
    bool fire_and_forget() const { fire_and_forget_; }
    int32_t src_id()const { return src_id_; }
    uint32_t method_id() const { return method_id_; }
    uint32_t session_id() const { return session_id_; }

    void set_fire_and_forget(bool fire_and_forget) { fire_and_forget_ = fire_and_forget_; }
    void set_src_id(int32_t src_id) { src_id_ = src_id; }
    void set_method_id(uint32_t method_id) { method_id_ = method_id; }
    void set_session_id(uint32_t session_id) { session_id_ = session_id; }

private:
    bool fire_and_forget_ = false; /** true: needn't response, false: need response */
    int32_t src_id_ = -1;
    uint32_t method_id_ = 0;
    uint32_t session_id_ = 0;
};


using MethodHandlePtr = std::shared_ptr<MethodHandle>;

using Method = std::function<void(MethodHandlePtr, const uint8_t* data, uint32_t len)>;
using ProxyMonitor = std::function<void(int32_t client_id, bool alive)>;

/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class CORE_CLASS Service
{
public:
    using CollieErrc = collie::core::CollieErrc;
    using ServiceAddrArray = std::vector<std::string>;
public:
    Service(const std::string& srv_name, uint32_t worker_cnt=1);
    virtual ~Service();

public:
    virtual CollieErrc RegisterMethod(uint32_t method_id, Method method);
    virtual CollieErrc SendResponse(MethodHandlePtr method_handle_ptr, const uint8_t* data, uint32_t len);
    virtual void EmitSignal(uint32_t signal_id, uint8_t* data, uint32_t len);
    virtual void SetProxyMonitor(ProxyMonitor monitor);

public:
    virtual CollieErrc Start();
    virtual CollieErrc Stop();
    virtual CollieErrc RegisterNetwrokBinding(NetworkBinding* binding);

public:
    virtual const std::string service_name();
    virtual ServiceAddrArray service_addr();


private:
    class Impl;
    std::unique_ptr<Impl> impl_;


};

} /** namespace com */
} /** namespace collie */

#endif /** !__COM_SERVICE_H__ */

